#!/usr/bin/env node

// 案例 命令增加 删除 编辑 查看

const { program } = require("commander");

const inquirer = require("inquirer");

const { version } = require("./package.json");

const { utils } = require("./utils")


// 定义交互列表
const promptList = [
    {
        type:"list",
        name:"list",
        message:"请选择",
        choices:["增加","编辑","删除","查看"]
    }
]

// 定义方法
const actionHandle = () => {
    // 把list 解析出来
    inquirer.prompt(promptList).then(({list}) => {
        // console.log(answers)

        switch (list) {
            case "查看":
                utils.createTable();
                break;
            case "增加":
                inquirer.prompt([{
                    type:"input",
                    name:"fileName",
                    message:"请输入新增文件名称"
                },{
                    type:"input",
                    name:"fileContent",
                    message:"请输入文件内容"
                }]).then(({fileName,fileContent}) => {
                    // console.log(answers)
                    utils.addFile(fileName,fileContent)
                })
                break;
            case "删除":
                inquirer.prompt([{
                    type:"list",
                    name:"fileName",
                    choices:[...utils.getfileList()],
                    message:"请选择要删除的文件"
                }]).then(({fileName})=>{
                    console.log(fileName);
                    utils.del(fileName)
                })

                break;

            case "编辑":
                inquirer.prompt([
                    {
                        type:"input",
                        name:"fileName",
                        message:"请输入要编辑的文件"
                    },
                    {
                        type:"input",
                        name:"newFileName",
                        message:"请输入新的名字"
                    }
                ]).then(({fileName,newFileName}) =>{
                    // console.log(answers)
                    utils.editFileName(fileName,newFileName)
                })
        }
    })
}


// 定义命令

program 
    .option("init")
    .description("file curd")
    .action(actionHandle);


// 解析进程参数

program.version(version).parse(process.argv)